#include <iostream>
#include "../comm/httplib.h"
#include "oj_Control.hpp"
#include <signal.h>

using namespace httplib;
using namespace oj_control;

    
static Control* control_ptr = nullptr;

void RecoverMachines(int signal)
{
    control_ptr->Recover();
}
int main()
{
    //ctrl + /  就让全部机器上线
    signal(SIGQUIT,RecoverMachines);

    Server server;
    Control control;

    control_ptr = &control;
    printf("mysql client Version: %s\n", mysql_get_client_info());
    // 获取全部的题目列表
    server.Get("/all_questions", [&control](const Request &req, Response &resp){ 
        std::string html;
        control.AllQuestions(&html);
        resp.set_content(html, "text/html;charset=utf-8"); 
        
        });

    // 打开一个题目(用户根据题目编号，提取题目内容s)
    server.Get(R"(/question/(\d+))", [&control](const Request &req, Response &resp){
        std::string num = req.matches[1];
        std::string html;
        control.OneQuestion(num,&html);
        resp.set_content(html,"text/html;charset=utf-8"); 
        
        });

    // 用户提交，我们判题（1.测试用例 2.Compiler_Run）
    server.Post(R"(/Judge/(\d+))", [&control](const Request &req, Response &resp){
        std::string num = req.matches[1];
        std::string result_json;
        //交给判题
        control.Judge(num,req.body,&result_json);
        //用户得到的:
        resp.set_content(result_json,"application/json;charset=utf-8");
        // resp.set_content("判题: " + num,"text/plain;charset=utf-8"); 
        });

    // 设定我的起始网页在哪个目录下
    server.set_base_dir("./wwwroot");

    server.listen("0.0.0.0", 8888);
    return 0;
}